package com.mf.interfaceTest;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.mf.mpos.pub.Controler;

public class ActionItems {
	private final Activity  activity;
	public ActionItems(  Activity  activity  )
	{
		this.activity = activity;
	}
	
	Object ret = null;
	Object WaitRet( Runnable uirunable){
		ret = null;
		
		activity.runOnUiThread( uirunable );
		
		while( ret == null ){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return (Integer) ret;
	}
	
	public int blockmsg(final String title, final String msg )
	{		
		Object s = WaitRet ( new Runnable() {			
			@Override
			public void run() {
				// TODO Auto-generated method stub				
				Builder dlg = new AlertDialog.Builder(activity);
				dlg.setTitle(title).setMessage(msg);
				
				DialogInterface.OnClickListener r = new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, final int arg1) {
						// TODO Auto-generated method stub						
						ret = arg1;
					}
				};				
				dlg.setPositiveButton("确认", r);
				dlg.show();				
			}
		});
		
		return (Integer) s;
	}
	public int select(final String title, final String... mList){
		
		return (Integer) WaitRet( new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				AlertDialog.Builder listDia=new AlertDialog.Builder(activity); 				
				listDia.setTitle(title); 
				listDia.setItems(mList, new DialogInterface.OnClickListener() { 					
					@Override  
					public void onClick(DialogInterface dialog, int which) {  
						// TODO Auto-generated method stub  						
						ret = which;
					}
				}); 				
				listDia.setCancelable(false);				
				listDia.create().show();
			}			
		});
		
	}
		

	
	
	ProgressDialog pdialog;	
	public void showwait(final String title, final String msg){
		
		activity.runOnUiThread( new Runnable() {			
			@SuppressWarnings("deprecation")
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if( pdialog==null ){
					pdialog = new ProgressDialog( activity );			
					pdialog.setButton("取消", new DialogInterface.OnClickListener() {  
				         public void onClick(DialogInterface dialog, int whichButton) { 
				        	 Controler.CancelComm();
				         }  
				    });
					pdialog.setTitle(title);
					pdialog.setMessage(msg);
					pdialog.show();
				}
				else{
					pdialog.setMessage(msg);
					pdialog.setTitle(title);			
				}
			}
		});
	}
	
	void closewait( ){
		activity.runOnUiThread( new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if( pdialog != null ){
					pdialog.dismiss();
					pdialog = null;
				}
			}
		});
	}

}
